<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px" clearable>
      <el-row>
        <el-form-item label="部件名称" prop="partname">
          <el-input
            v-model="queryParams.partname"
            placeholder="请输入部件名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input
            v-model="queryParams.specification"
            placeholder="请输入规格"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="部件类型" prop="type">
          <el-input
            v-model="queryParams.type"
            placeholder="请输入部件类型"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-row>

      <!-- <el-form-item label="数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-row>
        <el-form-item label="所在供应商名称" prop="suppliername">
          <el-input
            v-model="queryParams.suppliername"
            placeholder="请输入所在供应商名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="所在仓库名称" prop="warehousename">
          <el-input
            v-model="queryParams.warehousename"
            placeholder="请输入所在仓库名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="售价" prop="price">
            <el-input
              v-model="queryParams.price"
              placeholder="请输入售价"
              clearable
              @keyup.enter.native="handleQuery"
              oninput="value=value.replace(/[^\d.]/g,'')"
            />
          </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['partsell:partsell:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-shopping-cart-2"
          size="medium"
          :disabled="single"
          @click="handlebuy"
          v-hasPermi="['partsell:partsell:edit']"
        >购买</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['partsell:partsell:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="medium"
          @click="handleExport"
          v-hasPermi="['partsell:partsell:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partsellList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="数量">
                  <span>{{ props.row.quantity }}</span>
                </el-form-item>
                <el-form-item label="进价">
                  <span>{{ props.row.primeprice }}</span>
                </el-form-item>
                <el-form-item label="创建时间">
                  <span>{{ props.row.createtime }}</span>
                </el-form-item>
                <el-form-item label="修改时间">
                  <span>{{ props.row.modifytime }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
      <!-- <el-table-column label="部件ID" align="center" prop="partid" /> -->
      <el-table-column label="部件名称" align="center" prop="partname" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="部件类型" align="center" prop="type" />
      <el-table-column label="售价" align="center" prop="price" />
      <el-table-column label="所在供应商名称" align="center" prop="suppliername" />
      <el-table-column label="所在仓库名称" align="center" prop="warehousename" />
      <!-- <el-table-column label="数量" align="center" prop="quantity" /> -->
      <!-- <el-table-column label="进价" align="center" prop="primeprice" /> -->
     <!-- <el-table-column label="创建时间" align="center" prop="createtime" />
      <el-table-column label="修改时间" align="center" prop="modifytime" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            icon="el-icon-shopping-cart-2"
            @click="handlebuy(scope.row)"
            v-hasPermi="['partsell:partsell:edit']"
          >购买</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 购买部件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="销售员ID" prop="salespersonid">
          <el-select v-model="form.salespersonid" placeholder="请选择销售员ID" clearable filterable>
          	<el-option
          	v-for="(item,index) in saleoptions"
          	:key="item.index"
          	:label="item.salespersonid"
          	:value="item.salespersonid">
          	</el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="销售员ID" prop="salespersonid">
          <el-input v-model="form.salespersonid" placeholder="请输入销售员ID" />
        </el-form-item> -->
        <el-form-item label="客户ID" prop="customerid">
          <el-select v-model="form.customerid" placeholder="请选择客户ID" clearable filterable>
          	<el-option
          	v-for="(item,index) in customerList"
          	:key="item.index"
          	:label="item.custormerid"
          	:value="item.custormerid">
          	</el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="客户ID" prop="customerid">
          <el-input v-model="form.customerid" placeholder="请输入客户ID" />
        </el-form-item> -->
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactnumber">
          <el-input v-model="form.contactnumber" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPartsell, getPartsell, delPartsell, addPartsell, updatePartsell } from "@/api/partsell/partsell";
import { getcustomersList } from "@/api/customer/customer";
import { getSalespersons } from "@/api/salesperson/salesperson";


export default {
  name: "Partsell",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部件销售表格数据
      partsellList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierid: null,
        warehouseid: null,
        partname: null,
        quantity: null,
        price: null,
        specification: null,
        type: null,
        suppliername:null,
        warehousename:null
      },
      // 销售员信息
      saleoptions:{},
      // 客户信息
      customerList:{},
      //数量限制
      fquantity:0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {partname: [
          { required: true, message: "部件名称不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "数量不能为空", trigger: 'blur' },
          { validator:function(rule,value,callback){
            if(/^[0-9]*$/.test(value) == false){
                callback(new Error("请输入数字"));
            }else if(value < 1){
                callback(new Error("请输入大于0的数字"));
            }else if(value > 100){
                callback(new Error("请输入小于库存的数字"));
            }else{
              callback();
            }
          }, trigger: 'blur'
             },
        ],
        price: [
          { required: true, message: "售价不能为空", trigger: "blur" }
        ],
        specification: [
          { required: true, message: "规格不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "部件类型不能为空", trigger: "blur" }
        ],
        salespersonid:[
          { required: true, message: "销售员ID不能为空", trigger: "blur" }
        ],
        customerid:[
          { required: true, message: "客户ID不能为空", trigger: "blur" }
        ],
        contactnumber:[
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        address:[
          { required: true, message: "地址不能为空", trigger: "blur" }
        ]}
    };
  },
  created() {
    this.getList();
    this.customersList();
    this.getsalelist();
  },
  methods: {
    getsalelist(){
      getSalespersons().then(response=>{
        this.saleoptions=response.data;
      })},
    customersList(){
      getcustomersList().then(response=>{
        this.customerList=response.data;
      })
    },
    /** 查询部件销售列表 */
    getList() {
      this.loading = true;
      listPartsell(this.queryParams).then(response => {
        this.partsellList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partid: null,
        status: null,
        supplierid: null,
        warehouseid: null,
        customerid: null,
        salespersonid: null,
        partname: null,
        quantity: null,
        primeprice: null,
        price: null,
        specification: null,
        type: null,
        createtime: null,
        modifytime: null,
        deleted: null,
        fquantity:null,
        contactnumber:null,
        address:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加部件销售";
    // },
    /** 购买按钮操作 */
    handlebuy(row) {
      this.reset();
      const partid = row.partid || this.ids;
      getPartsell(partid).then(response => {
        this.form = response.data;
        this.form.fquantity=this.form.quantity;
        this.rulereast(this.form.quantity);
        this.open = true;
        this.title = "部件销售";
      });

    },
    rulereast(fquantity){
      this.rules.quantity=[
        { required: true, message: "数量不能为空", trigger: 'blur' },
        { validator:function(rule,value,callback){
          if(/^[0-9]*$/.test(value) == false){
              callback(new Error("请输入数字"));
          }else if(value < 1){
              callback(new Error("请输入大于0的数字"));
          }else if(value > fquantity){
              callback(new Error("请输入不大于库存的数字"));
          }else{
            callback();
          }
        }, trigger: 'blur'
           }
      ];
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partid != null) {
            if(this.form.fquantity==this.form.quantity){
            updatePartsell(this.form).then(response => {
              this.$modal.msgSuccess("购买全部成功");
              this.open = false;
              this.getList();

            });
            }
            else if(this.form.fquantity>this.form.quantity){
              console.info(this.form.quantity);
              addPartsell(this.form).then(response => {
                console.info(this.form.quantity);
                this.$modal.msgSuccess("购买成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    // /** 删除按钮操作 */
    // handleDelete(row) {
    //   const partids = row.partid || this.ids;
    //   this.$modal.confirm('是否确认删除部件销售编号为"' + partids + '"的数据项？').then(function() {
    //     return delPartsell(partids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download('partsell/partsell/export', {
        ...this.queryParams
      }, `partsell_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #515a6e;
  }
  .demo-table-expand .el-form-item {
    margin-left: 65px;
    margin-right: 0;
    margin-bottom: 0;
    width: 100%;
  }
</style>
